import sdk from 'redcatSdk'

// 多个表单放在一起定制也可以
export default {
  'form-id1': async (context) => {
    const res = await sdk.http.get('/example-api/accounts')
    const accounts = res.data

    if (accounts.length) {
      sdk.message(`成功获取到 ${accounts.length} 条账户记录.`)
    } else {
      sdk.message.error('获取失败')
    }

    context.$on('button.click', ({ button }) => {
      if (button.id === 'button-id') {
        const id = sdk.utils.id.create()

        context.form.dataService.data['data-id-1'] = id
        context.form.dataService.data['data-id-2'] = accounts.length
      }

      sdk.router.go(-1)
    })
  },
  'form-id2': async (context) => {
    const res = await sdk.http.get('/example-api/accounts')
    const accounts = res.data

    if (accounts.length) {
      sdk.message(`成功获取到 ${accounts.length} 条账户记录.`)
    } else {
      sdk.message.error('获取失败')
    }

    context.$on('button.click', ({ button }) => {
      if (button.id === 'button-id') {
        const id = sdk.utils.id.create()

        context.form.dataService.data['data-id-1'] = id
        context.form.dataService.data['data-id-2'] = accounts.length
      }

      sdk.router.go(-1)
    })
  }
}
